package com.zq.verify;

import com.zq.annotation.type.CheckPermissionType;
import com.zq.annotation.type.CheckRolesType;

import java.util.List;

public class VerifyConfigurer implements Verify {

    public List<String> getRoles(){
        return null;
    }

    public List<String> getPermissions(){
        return null;
    }

    /**
     * 校验用户是否拥有角色
     * @param roles
     * @param type
     * @return
     */
    @Override
    public boolean verifyRoles(String[] roles, CheckRolesType type) {
        if(roles==null || roles.length==0) return true;
        final List<String> rolesList = getRoles();
        if(rolesList==null || rolesList.size()==0) return false;
        if(type==CheckRolesType.OR)
            return checkOR(roles,rolesList);
        return checkAND(roles ,rolesList);
    }

    /**
     * 校验用户是否拥有权限
     * @param permissions
     * @param type
     * @return
     */
    @Override
    public boolean verifyPermissions(String[] permissions, CheckPermissionType type) {
        if(permissions ==null || permissions.length==0) return true;
        final List<String> permissionsList = getPermissions();
        if(permissionsList ==null || permissionsList.size()==0) return false;
        if(type==CheckPermissionType.OR)
            return checkOR(permissions,permissionsList );
        return checkAND(permissions,permissionsList);
    }

    public boolean checkOR(String[] src, List<String> target) {
        for (String role : src) {
            if (target.contains(role)) {
                return true;
            }
        }
        return false;
    }

    public boolean checkAND(String[] src, List<String> target){
        for (String role : src) {
            if (!target.contains(role)) {
                return false;
            }
        }
        return true;
    }
}
